package logic

import (
	"context"
	"errors"
	"shop_srv/internal/svc"
	"shop_srv/models"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *shop.UserUpdateRequest) (*shop.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var u models.User

	if err = u.GetUserByID(in.UserId); err != nil {
		logx.Error("用户信息获取失败")
		return nil, errors.New("用户信息获取失败")
	}
	if u.ID == 0 {
		logx.Error("该用户不存在")
		return nil, errors.New("该用户不存在")
	}

	u.Images = in.Images
	u.NickName = in.NickName
	u.Email = in.Email
	if err = u.UpdatesUserInfo(int(in.UserId)); err != nil {
		logx.Error("用户信息更新失败")
		return nil, errors.New("用户信息更新失败")
	}

	return &shop.UserUpdateResponse{
		UserId: int64(u.ID),
	}, nil
}
